import * as Mixly from 'mixly';

export const communicate_ir_recv = function (_, generator) {
    generator.definitions_['import_irremote'] = 'import irremote_no';
    var pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    return ["irremote_no.read_id(" + pin + ")", generator.ORDER_ATOMIC];
}

export const E6_send = function (_, generator) {
    generator.definitions_['import ustruct'] = 'import ustruct';
    var dropdown_uart = this.getFieldValue('mode');
    var num1 = generator.valueToCode(this, 'num1', generator.ORDER_ATOMIC);
    var num2 = generator.valueToCode(this, 'num2', generator.ORDER_ATOMIC);
    var code = 'uart' + dropdown_uart + '.write(ustruct.pack("<bbbbbbbbbbbbbbbbbbbb",0x56,0xAB,0x14,0xE8,0x13,int(' + num1 + '),int(' + num2 + '),0,0,0,0,0,0,0,0,0,0,0,0,0xCF))\n';
    return code;
}

export const communicate_i2c_init = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    generator.definitions_['from machine import I2C'] = 'from machine import I2C';
    var freq = generator.valueToCode(this, 'freq', generator.ORDER_ATOMIC);
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    return "" + sub + " = I2C(I2C.I2C0, freq=" + freq + ", scl=30, sda=31)\n";
}

export const communicate_i2c_init1 = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    generator.definitions_['from machine import I2C'] = 'from machine import I2C';
    var freq = generator.valueToCode(this, 'freq', generator.ORDER_ATOMIC);
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var sda = generator.valueToCode(this, 'RX', generator.ORDER_ATOMIC);
    var scl = generator.valueToCode(this, 'TX', generator.ORDER_ATOMIC);
    return "" + sub + " = I2C(I2C.I2C1, freq=" + freq + ", scl=board.pin_D[" + scl + "], sda=board.pin_D[" + sda + "])\n";
}

export const communicate_i2s_init = function (_, generator) {
    generator.definitions_['from Maix import I2S'] = 'from Maix import I2S';
    generator.definitions_['from fpioa_manager import fm'] = 'from fpioa_manager import fm';
    var mode = this.getFieldValue('mode');
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var code = '';
    if (mode == '1') {
        code += "fm.register(20,fm.fpioa.I2S0_IN_D0)\n";
        code += "fm.register(19,fm.fpioa.I2S0_WS)\n";
        code += "fm.register(18,fm.fpioa.I2S0_SCLK)\n";
        code += sub + " = I2S(I2S.DEVICE_0)\n";
    }
    else {
        code += "fm.register(34,fm.fpioa.I2S2_OUT_D1)\n";
        code += "fm.register(35,fm.fpioa.I2S2_SCLK)\n";
        code += "fm.register(33,fm.fpioa.I2S2_WS)\n";
        code += sub + " = I2S(I2S.DEVICE_2)\n";
    }
    return code;
}

export const communicate_i2s_config = function (_, generator) {
    generator.definitions_['from Maix import I2S'] = 'from Maix import I2S';
    generator.definitions_['from fpioa_manager import fm'] = 'from fpioa_manager import fm';
    var mode = this.getFieldValue('mode');
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var sample = generator.valueToCode(this, 'sample', generator.ORDER_ATOMIC);
    var code = '';
    if (mode == '1') {
        code = "" + sub + ".channel_config(" + sub + ".CHANNEL_0, I2S.RECEIVER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.STANDARD_MODE)\n";
    }
    else {
        code = "" + sub + ".channel_config(" + sub + ".CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)\n";
    }
    code += "" + sub + ".set_sample_rate(" + sample + ")\n";
    return code;
}


export const communicate_i2s_init1 = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    generator.definitions_['from Maix import I2S'] = 'from Maix import I2S';
    var mode = this.getFieldValue('mode');
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var sample = generator.valueToCode(this, 'sample', generator.ORDER_ATOMIC);
    var code = '';
    if (mode == '1') {
        code += "board.register(21,board.FPIOA.I2S0_IN_D0)\n";
        code += "board.register(20,board.FPIOA.I2S0_WS)\n";
        code += "board.register(19,board.FPIOA.I2S0_SCLK)\n";
        code += sub + " = I2S(I2S.DEVICE_0)\n";
        code += sub + ".channel_config(" + sub + ".CHANNEL_0, I2S.RECEIVER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.STANDARD_MODE)\n";
    }
    else {
        code += "board.register(26,board.FPIOA.I2S0_OUT_D1)\n";
        code += "board.register(27,board.FPIOA.I2S0_SCLK)\n";
        code += "board.register(25,board.FPIOA.I2S0_WS)\n";
        code += sub + " = I2S(I2S.DEVICE_0)\n";
        code += sub + ".channel_config(" + sub + ".CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT ,cycles = I2S.SCLK_CYCLES_32, align_mode = I2S.RIGHT_JUSTIFYING_MODE)\n";
    }
    code += sub + ".set_sample_rate(" + sample + ")\n";
    return code;
}
//--------新增-------------------------------------------------------

//--开始--------------------------Amigo硬件 -20210920-------------------------------------------------

export const communicate_amigo_i2c_init = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    generator.definitions_['from machine import I2C'] = 'from machine import I2C';
    var freq = generator.valueToCode(this, 'freq', generator.ORDER_ATOMIC);
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    return "" + sub + " = I2C(I2C.I2C3, freq=" + freq + ", scl=24, sda=27)\n";
}

export const communicate_amigo_i2c_init1 = function (_, generator) {
    generator.definitions_['import board'] = 'import board';
    generator.definitions_['from machine import I2C'] = 'from machine import I2C';
    var freq = generator.valueToCode(this, 'freq', generator.ORDER_ATOMIC);
    var sub = generator.valueToCode(this, 'SUB', generator.ORDER_ATOMIC);
    var sda = generator.valueToCode(this, 'RX', generator.ORDER_ATOMIC);
    var scl = generator.valueToCode(this, 'TX', generator.ORDER_ATOMIC);
    return "" + sub + " = I2C(I2C.I2C0, freq=" + freq + ", scl=" + scl + ", sda=" + sda + ")\n";
}

//---------------XXBIT板载I2C通信资源---20230520--------------------
export const communicate_xxbit_i2c_init = function (_, generator) {
    var version = Mixly.Boards.getSelectedBoardKey().split(':')[0]
    generator.definitions_['from ' + version + ' import *'] = 'from ' + version + ' import *';
    var code = 'ob_i2c';
    return [code, generator.ORDER_ATOMIC];
}

export const communicate_i2c_read = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return ["" + name + ".readfrom(" + address + ", " + data + ")", generator.ORDER_ATOMIC];
}

export const communicate_i2c_write = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return "" + name + ".writeto(" + address + ", " + data + ")\n";
}

export const communicate_i2c_scan = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    return ["" + name + ".scan()", generator.ORDER_ATOMIC];
}

export const communicate_i2c_master_read = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + name + ".read()";
    return [code, generator.ORDER_ATOMIC];
}

export const communicate_i2c_available = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + name + ".available()";
    return [code, generator.ORDER_ATOMIC];
}

export const i2c_slave_onreceive = function (_, generator) {
    var pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_i2c_' + pin] = 'Wire.begin(' + pin + ');';
    generator.setups_['setup_i2c_onReceive_' + pin] = 'Wire.onReceive(i2cReceiveEvent_' + pin + ');';
    var funcName = 'i2cReceiveEvent_' + pin;
    var branch = generator.statementToCode(this, 'DO');
    var code2 = 'void' + ' ' + funcName + '(int howMany) {\n' + branch + '}\n';
    generator.definitions_[funcName] = code2;
    return '';
}

export const communicate_spi_init = function (_, generator) {
    generator.definitions_['import_machine'] = 'import machine';
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var freq = generator.valueToCode(this, 'freq', generator.ORDER_ATOMIC);
    var polarity = generator.valueToCode(this, 'polarity', generator.ORDER_ATOMIC);
    var phase = generator.valueToCode(this, 'phase', generator.ORDER_ATOMIC);
    var mosi = generator.valueToCode(this, 'mosi', generator.ORDER_ATOMIC);
    var miso = generator.valueToCode(this, 'miso', generator.ORDER_ATOMIC);
    var sck = generator.valueToCode(this, 'sck', generator.ORDER_ATOMIC);
    return "" + name + " = machine.SPI(baudrate=" + freq + ", polarity=" + polarity + ", phase=" + phase + ", sck=machine.Pin(" + sck + "), mosi=machine.Pin(" + mosi + "), miso=machine.Pin(" + miso + "));\n";
}

export const communicate_spi_set = function (_, generator) {
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return "spi.init(baudrate=" + data + ")\n";
}

export const communicate_spi_buffer = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return "" + varname + "=bytearray(" + data + ")\n";
}

export const communicate_spi_read = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return ["" + varname + ".read(" + data + ")", generator.ORDER_ATOMIC];
}

export const communicate_spi_read_output = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    var val = generator.valueToCode(this, 'val', generator.ORDER_ATOMIC);
    return ["" + varname + ".read(" + data + "," + val + ")", generator.ORDER_ATOMIC];
}

export const communicate_spi_readinto = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return ["" + varname + ".readinto(" + data + ")", generator.ORDER_ATOMIC];
}

export const communicate_spi_readinto_output = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    var val = generator.valueToCode(this, 'val', generator.ORDER_ATOMIC);
    return ["" + varname + ".readinto(" + data + "," + val + ")", generator.ORDER_ATOMIC];
}

export const communicate_spi_write = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    return ["" + varname + ".write(" + data + ".encode('utf-8'))", generator.ORDER_ATOMIC];
}

export const communicate_spi_write_readinto = function (_, generator) {
    var varname = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var data = generator.valueToCode(this, 'data', generator.ORDER_ATOMIC);
    var val = generator.valueToCode(this, 'val', generator.ORDER_ATOMIC);
    // var op=this.getFieldValue('op');
    // if(op=="byte"){
    return ["" + varname + ".write_readinto(" + data + ".encode('utf-8')," + val + ")", generator.ORDER_ATOMIC];
    // }else{
    //   return [""+varname+".write_readinto(" + data + ","+val+")", generator.ORDER_ATOMIC];
    // }
}

export const communicate_ow_init = function (_, generator) {
    generator.definitions_['import_machine'] = 'import machine';
    generator.definitions_['import_onewire'] = "import onewire";
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var varName = generator.valueToCode(this, 'BUS', generator.ORDER_ATOMIC);
    var code = "" + name + "=onewire.OneWire(machine.Pin(" + varName + "))\n";
    return code;
}

export const communicate_ow_scan = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + name + ".scan()";
    return [code, generator.ORDER_ATOMIC];
}

export const communicate_ow_reset = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + name + ".reset()\n";
    return code;
}

export const communicate_ow_read = function (_, generator) {
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + name + ".readbyte()";
    return [code, generator.ORDER_ATOMIC];
}

export const communicate_ow_write = function (_, generator) {
    var varName = generator.valueToCode(this, 'byte', generator.ORDER_ATOMIC);
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var op = this.getFieldValue('op');
    var code = "" + name + "." + op + "(" + varName + ")\n";
    return code;
}

export const communicate_ow_select = function (_, generator) {
    var varName = generator.valueToCode(this, 'byte', generator.ORDER_ATOMIC);
    var name = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var code = "" + name + ".select_rom(" + varName + ".encode('utf-8'))\n";
    return code;
}